#include "gptapi.h"

#define MODE_NAME "deepseek-v3-free"
#define REQUIRE "你是一个资深程序员，给用户的代码一点提示，指明其代码漏洞，让其写的更好，语言要简洁，以中文文本作为回复内容的组成"

int parameter_check(int argc, char* argv[]);  // 检查参数
void myhelp();
int code_read(char** buf, char* path);
int send_msg_to_gpt(char* model_name, char* require, char* path);
int get_gpt_name(char* insert);

int main(int argc, char* argv[]) {
    int ret = 0;
    if((ret = parameter_check(argc, argv)) != 0){
        mylog(ret, argv);
    }
    //gpt_read_code(MODE_NAME, REQUIRE, "你好", 0);
    return 0;
}

int parameter_check(int argc, char* argv[]){
    if(argc > 1){
    
        if(!strcmp(argv[1], "--help")) {
            myhelp();
            return 0;
        }

        if(access(argv[1], F_OK) != 0){
            return FILE_NOT_FOUND;
        }

        if(argc == 2){ // 默认参数
            int ret = 0;
            if((ret = send_msg_to_gpt(MODE_NAME, REQUIRE, argv[1])) != 0) return ret;
        }

        if(argc == 3){
            int ret = 0;
            if((ret = get_gpt_name(argv[2])) < 0) return MODEL_ERROR;
            if(ret == 0){
                if((ret = send_msg_to_gpt(MODE_NAME, argv[2], argv[1])) != 0) return ret;
            }else{
                if((ret = send_msg_to_gpt(gpt_name[ret], REQUIRE, argv[1])) != 0) return ret;
            }
        }

        if(argc == 4){
            int ret = 0;
            if((ret = get_gpt_name(argv[2])) <= 0) return MODEL_ERROR;
            if((ret = send_msg_to_gpt(gpt_name[ret], argv[3], argv[1])) != 0) return ret;
        }


        return 0;
    }

    return FEW_PARAMETERS;
}

int get_gpt_name(char* insert){
    int mode = 0;
    if(strncmp(insert, "-g", 2) == 0) mode = 1;
    if(strncmp(insert, "-q", 2) == 0) mode = 2;
    if(insert[0] == '-' && mode == 0) return -1;
    return mode;
}

int send_msg_to_gpt(char* model_name, char* require, char* path){
    // 1.读取argv[1]文件的代码
    char* buffer = NULL;
    int ret = 0;
    if((ret = code_read(&buffer, path)) != 0){
        if(ret == -1) return OPEN_FILE_ERROR;
        if(ret == -2) return FILE_NULL;
        if(ret == -3) return FILE_READ_ERROR;
    }
    // 2.发送给gpt
    gpt_read_code(model_name, require, buffer, 0);
    // 3.释放内存
    free(buffer);

    return 0;
}

int code_read(char** buf, char* path){
    FILE* fp = fopen(path, "rb");
    if(fp == NULL) return -1;

    fseek(fp, 0, SEEK_END);
    uint64_t file_size = ftell(fp);
    rewind(fp);

    *buf = (char* )malloc(file_size + 1);
    //printf("size: %ld \n", file_size);
    if(*buf == NULL){
        fclose(fp);
        return -1;
    }
    size_t read_size = fread(*buf, 1, file_size, fp);
    if(read_size == 0){
        fclose(fp);
        return -2;
    }
    if(read_size != file_size){
        fclose(fp);
        return -3;
    }
    (*buf)[read_size] = 0;  // *buf[file_size]容易产生段错误
    fclose(fp);
    return 0;
}

void myhelp(){
    printf("用法: checkcode [文件路径]\n");
    printf("      checkcode [文件路径] [用户输入]\n");
    printf("      checkcode [文件路径] [模型] [用户输入]\n");
    printf("[文件路径]: 文件所处位置\n");
    printf("[用户输入]: 用户需求信息，无则默认检查代码的逻辑问题\n");
    printf("[模型]: 无则默认deepseek 可选模型 [-g] 智普 [-q] 千问\n");
}



 